package model;

import java.util.Arrays;

/**
 * Represent the login model portion of the system.
 * For now, the data is not persistence - we will
 * use one or two test cases as demonstration.
 * 
 * @author Kenny Chong, Dan End, James Ju
 * CSE 333 : User Interface
 * Project HMSystem
 */
public class LoginModel 
{
	// Test Names and Passwords.
	private final String managerName = "Kenny";
	private final String managerPassword = "1234x";
	
	private final String coManagerName = "James";
	private final String coManagerPassword = "Hello";
	
	/**
	 * Construct LoginModel.
	 */
	public LoginModel(){}
	
	/**
	 * Verify Name and Password.
	 */
	public boolean verifyUser(String name, char[] password)
	{
		// Simple identification.
		boolean verify1 = (name.equals(managerName) && this.isPasswordCorrect(password));
		boolean verify2 = (name.equals(coManagerName) && this.isPasswordCorrect(password));
		
		return (verify1 || verify2);
	}
	
	/**
     * Checks the passed-in array against the correct password.
     * After this method returns, you should invoke eraseArray
     * on the passed-in array.
     */
    private boolean isPasswordCorrect(char[] input) 
    {
        boolean isCorrect = true;
        // The manager password.
        char[] mPassword = managerPassword.toCharArray();
        
        // The co-manager password.
        char[] coPassword = coManagerPassword.toCharArray();
 
		if ((input.length != mPassword.length)
				|| (input.length != coPassword.length)) 
		{
			isCorrect = false;
		} else {
			isCorrect = (Arrays.equals(input, mPassword) || Arrays.equals(
					input, coPassword));
        }
 
        //Zero out the passwords.
        Arrays.fill(coPassword,'0');
        Arrays.fill(mPassword, '0');
 
        return isCorrect;
    }
}
